package com.yc.repay.controller;

import com.yc.pay.api.enums.PayWayEnums;
import com.yc.pay.api.req.NetBankPayReq;
import com.yc.pay.api.resp.NetBankPayData;
import com.yc.pay.api.resp.RespVo;
import com.yc.pay.api.utils.IdWorker;
import com.yc.pay.api.utils.PayDigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * 还款接口 - 调用支付服务
 */
@RestController
@RequestMapping("/repay")
public class RepayController {


    @Autowired
    private RestTemplate restTemplate;


    /**
     * 主动还款
     */
   @RequestMapping("/test")
    public void volRepay(HttpServletRequest request){

        //还款，调用支付服务
       String url = "http://127.0.0.1:8081/pay/netBankPay";

       NetBankPayReq netBankPayReq = new NetBankPayReq();

       String mchId = "1001";  //读取配置中心

       String key = "reqtest1234";

       IdWorker idWorker = new IdWorker(1,1,1);

       String orderNo = idWorker.nextId() +"";

       String amount = "100";

       String subject = "测试订单";

       String payChannel = "alipay";

       String payWay = PayWayEnums.ALIPAY_WAP_PAY.getCode();

       netBankPayReq.setAmount(amount);
       netBankPayReq.setOrderNo(orderNo);
       netBankPayReq.setSubject(subject);
       netBankPayReq.setMchId(mchId);
       netBankPayReq.setPayWay(payWay);
       netBankPayReq.setQuitUrl("www.baidu.com");
       netBankPayReq.setReturnUrl("www.taobao.com");

       try {
           String sign = PayDigestUtil.getSign(netBankPayReq,key);
           netBankPayReq.setSign(sign);
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       }

       //通过http调用支付服务
       RespVo respVo = restTemplate.postForObject(url,netBankPayReq, RespVo.class);

       //解析

       NetBankPayData payData = (NetBankPayData)respVo.getData();

       System.out.println("-------------payData-" + payData);



    }


}
